//
//  EducationVC.m
//  BrushFace
//
//  Created by Harry on 16/1/27.
//  Copyright © 2016年 Zzz. All rights reserved.
//
#import "MeRequest.h"
#import "SchoolVC.h"
#import "EduModel.h"
#import "CustomPickerView.h"
#import "EducationVC.h"
#import "UserDefaultCell.h"
#import "EduSureView.h"
#import "DatePickerView.h"
@interface EducationVC ()<UITableViewDelegate,UITableViewDataSource>{
    EduSureView *sureView;
}
@property (nonatomic,strong)CustomPickerView *myPicker;
@property (nonatomic,strong)DatePickerView *datePicker;
@property (nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation EducationVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navBar setTitle:@"学历学籍"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    _dataArray = [NSMutableArray array];
    [self creatDataArray];
    _tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    sureView = [EduSureView loadBundleNib];
    sureView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 200);
    [sureView.commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
    _tableView.tableFooterView = sureView;
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    NSMutableDictionary *params = [self configParams];
    
    if (params==nil)
    {
        sureView.commitBtn.backgroundColor = [UIColor lightGrayColor];
        return;
    }else{
        sureView.commitBtn.backgroundColor = ColorDefaultBlue;
    }
}
-(void)creatDataArray
{
    for (int i = 0; i<2; i++)
    {
        NSMutableArray *array = [[NSMutableArray alloc]init];
        [_dataArray addObject:array];
    }
    EduModel *model = [[EduModel alloc]init];
    model.title = @"地区";
    [_dataArray[0] addObject:model];
    
    EduModel *model1 = [[EduModel alloc]init];
    model1.title = @"院校名称";
    [_dataArray[0] addObject:model1];
    
    EduModel *model2 = [[EduModel alloc]init];
    model2.title = @"当前状态";
    [_dataArray[1] addObject:model2];
    
}

#pragma mark --数据提交
-(void)commitBtnClick
{
    NSMutableDictionary *params = [self configParams];
   
       if (params==nil)
    {
        return;
    }
    
   __weak typeof(self) weakSelf = self;
    [MeRequest sendUserDataWithType:education AndUserData:params success:^(NSDictionary *dic)
    {
        NSLog(@"%@",dic);
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *string = [NSString stringWithFormat:@"%@",dic[@"result"]];
            if ([string intValue]==1)
            {
                NSString *str = [weakSelf getResultString];
                _result([str copy]);
                [self.navigationController popViewControllerAnimated:YES];
            }});
    } error:^(NSError *error) {
        
    }];

}
-(NSMutableString *)getResultString
{
    NSMutableString *resultStr = [[NSMutableString alloc]init];
    NSArray *array1 = _dataArray[2];
    EduModel *model1 = array1[0];
    [resultStr appendString:model1.value];
    
    [resultStr appendString:@"+"];
    NSArray *array = _dataArray[0];
    EduModel *model = array[1];
    [resultStr appendString:model.value];
    return resultStr;
}
-(NSMutableDictionary *)configParams
{
    NSMutableArray *configArray = [NSMutableArray array];
    for (NSArray *array in _dataArray)
    {
        for (EduModel *model in array)
        {
            if (model.value ==nil)
            {
                return nil;
            }
            [configArray addObject:model];
        }
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setObject:_addressDic[@"value"] forKey:@"schoolprovince"];
//    [params setObject:@"34" forKey:@"schoolprovince"];
    [params setObject:@(0) forKey:@"schoolcity"];
    [params setObject:_schoolDic[@"value"] forKey:@"schoolname"];
//    [params setObject:@"1" forKey:@"schoolname"];
    EduModel *model = configArray[3];
    if ([model.value isEqualToString:@"高中以下"])
    {
        [params setObject:@(1) forKey:@"education"];
    }
    else if ([model.value isEqualToString:@"专科"])
    {
    [params setObject:@(2) forKey:@"education"];
    }
    else if ([model.value isEqualToString:@"本科"])
    {
        [params setObject:@(3) forKey:@"education"];
    }
    else if ([model.value isEqualToString:@"硕士"])
    {
        [params setObject:@(4) forKey:@"education"];
    }
    else if ([model.value isEqualToString:@"博士及以上"])
    {
        [params setObject:@(5) forKey:@"education"];
    }
    else if ([model.value isEqualToString:@"其他"])
    {
        [params setObject:@(6) forKey:@"education"];
    }
   
    
    
    
    
    
    
    EduModel *model2 = [configArray objectAtIndex:2];
    EduModel *model4 = [configArray objectAtIndex:4];
    if ([model2.value isEqualToString:@"毕业"])
    {
        [params setObject:@(1) forKey:@"status"];
        [params setObject:model4.value forKey:@"graduatetime"];
        [params setObject:@"" forKey:@"startinschooltime"];
    }
    else if ([model2.value isEqualToString:@"在读"])
    {
         [params setObject:@(2) forKey:@"status"];
        [params setObject:model4.value forKey:@"startinschooltime"];
        [params setObject:@""forKey:@"graduatetime"];
    }
    
    

    
    return params;
}

#pragma mark-UITableView--Delegate  DataSource
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return _dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    return [_dataArray[section] count];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellName = @"cell";
    UserDefaultCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (cell == nil) {
        cell = [UserDefaultCell loadBundleNib];
    }
    NSArray *array = _dataArray[indexPath.section];
    EduModel *model = array[indexPath.row];
    cell.userCellTitle.text = model.title;
    cell.userCellDefault.text = model.value;
    return cell;

}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return 10;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0)
    {
        
            SchoolVC *schoolVC = [[SchoolVC alloc]init];
        if (indexPath.row == 0)
        {
            schoolVC.SCHOOLTYPE = SCHOOLTYPE_adress;
        }
        else
        {
            schoolVC.SCHOOLTYPE = SCHOOLTYPE_school;

        }
            [self.navigationController pushViewController:schoolVC animated:YES];
        
    }
    else if (indexPath.section ==1)
    {
    [self creatPickWithSty:EducationStatus andindexPath:indexPath];
    }
    else if (indexPath.section ==2)
    {
        [self creatPickWithSty:HighEducation andindexPath:indexPath];
    }
    else if (indexPath.section ==3)
    {
        [self creatPickWithindexPath:indexPath];
    }


}

-(void)creatPickWithindexPath:(NSIndexPath *)indexPath
{
    if (!_datePicker) {
        _datePicker = [DatePickerView loadBundleNib];
        typeof(self) weakself = self;
        _datePicker.result = ^(NSString *resultStr)
        {
            
            [weakself eduStatusChangeWithDataStr:resultStr index:indexPath];
            
        };
        _datePicker.cancelResult = ^()
        {
            _datePicker = nil;
        };
        [self.view addSubview:_datePicker];
        
        _datePicker.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 216);
        [UIView animateWithDuration:1 animations:^{
            _datePicker.frame = CGRectMake(0, SCREEN_HEIGHT-216, SCREEN_WIDTH, 216);
            _datePicker = nil;
        }];

    }
}
#pragma mark--创建Picker
-(void)creatPickWithSty:(pickerType)style andindexPath:(NSIndexPath *)indexPath
{
    if (_datePicker != nil) {
        [_datePicker removeFromSuperview];
    }
    CustomPickerView *pickView = [[[NSBundle mainBundle]loadNibNamed:@"CustomPickerView" owner:self options:nil]lastObject];
    pickView.pickerStyle = style;
    
    [pickView getResult:^(NSString *resultStr)
     {
         _myPicker = nil;
         [self eduStatusChangeWithDataStr:resultStr index:indexPath];
         
     }];
    pickView.cancelResult = ^(){
        _myPicker = nil;
    };
    
    [self.view addSubview:pickView];
    self.myPicker = pickView;
    pickView.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, 216);
    [UIView animateWithDuration:1 animations:^{
        pickView.frame = CGRectMake(0, SCREEN_HEIGHT-216, SCREEN_WIDTH, 216);
    }];
    
}

-(void)eduStatusChangeWithDataStr:(NSString *)str index:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1)
    {
        NSArray *array = _dataArray[indexPath.section];
        EduModel *model = array[0];
        model.value = str;
        
        if (_dataArray.count>2)
        {
            while (_dataArray.count>2) {
                [_dataArray removeObjectAtIndex:_dataArray.count-1];
            }
        }
        if ([str isEqualToString:@"在读"])
        {
            
            EduModel *modelZ = [[EduModel alloc]init];
            modelZ.title = @"学历";
            NSMutableArray *arrayZ = [[NSMutableArray alloc]init];
            [arrayZ addObject:modelZ];
            [_dataArray addObject:arrayZ];
            
            EduModel *modelZ1 = [[EduModel alloc]init];
            modelZ1.title = @"入学时间";
            NSMutableArray *arrayZ1 = [[NSMutableArray alloc]init];
            [arrayZ1 addObject:modelZ1];
            [_dataArray addObject:arrayZ1];
            
        }
        else if ([str isEqualToString:@"毕业"])
        {
            
            EduModel *modelB = [[EduModel alloc]init];
            modelB.title = @"最高学历";
            NSMutableArray *arrayB = [[NSMutableArray alloc]init];
            [arrayB addObject:modelB];
            [_dataArray addObject:arrayB];
            
            EduModel *modelB1 = [[EduModel alloc]init];
            modelB1.title = @"毕业时间";
            NSMutableArray *arrayB1 = [[NSMutableArray alloc]init];
            [arrayB1 addObject:modelB1];
            [_dataArray addObject:arrayB1];
            
            
        }

    }
    else
    {
        NSArray *array =_dataArray[indexPath.section];
        EduModel *model = [array objectAtIndex:indexPath.row];
        model.value = str;
        UserDefaultCell *cell = [_tableView cellForRowAtIndexPath:indexPath];
        cell.userCellDefault.text = str ;
    
    }
    [_tableView reloadData];
    NSMutableDictionary *params = [self configParams];
    
    if (params==nil)
    {
        sureView.commitBtn.backgroundColor = [UIColor lightGrayColor];
        return;
    }else{
        sureView.commitBtn.backgroundColor = ColorDefaultBlue;
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)setSchoolDic:(NSMutableDictionary *)schoolDic
{
    _schoolDic = schoolDic;
    NSIndexPath *index = [NSIndexPath indexPathForRow:1 inSection:0];
    NSArray *array =_dataArray[index.section];
    EduModel *model = [array objectAtIndex:index.row];
    model.value = _schoolDic[@"name"];
    
    UserDefaultCell *cell = [_tableView cellForRowAtIndexPath:index];
    cell.userCellDefault.text = _schoolDic[@"name"];
    
}
-(void)setProName:(NSString *)proName
{
    _proName = proName;
    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray *array =_dataArray[index.section];
    EduModel *model = [array objectAtIndex:index.row];
    model.value = _proName;
    
    UserDefaultCell *cell = [_tableView cellForRowAtIndexPath:index];
    cell.userCellDefault.text = _proName;
}
-(void)setAddressDic:(NSMutableDictionary *)addressDic
{
    _addressDic = addressDic;
//    NSIndexPath *index = [NSIndexPath indexPathForRow:0 inSection:0];
//    NSArray *array =_dataArray[index.section];
//    EduModel *model = [array objectAtIndex:index.row];
//    model.value = _addressDic[@"name"];
//
//    UserDefaultCell *cell = [_tableView cellForRowAtIndexPath:index];
//    cell.userCellDefault.text = _addressDic[@"name"];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
